// index.js
// 日记聚合页


import util from '../../../utils/util.js';
var app = getApp();
import { getObjectFromStorgeSync, KEYS } from '../../../utils/http.js';

Page({

  data: {
    infolist:[],
    type: 0,
    active:0,
  },

  onChange(event) {
    this.setData({
      type: event.detail.index,
      active: event.detail.index ,
    }, () => {
      this.getlist();
    });
  },

  onShow: function () {
    this.getlist();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(options) {
    wx.setNavigationBarTitle({
      title: '我的工单'
    });
  },

  showDetail: function (obj) {
    var that = this;
    var objkey = parseInt(obj.currentTarget.id);
    var ret = this.data.infolist[objkey];
    let data = JSON.stringify(ret);
    wx.navigateTo({
      url: '../workorderdetail/workorderdetail?data=' + data,
    });
  },

  bindTap1: function () {
    var that = this;
    this.setData({
      type: 0,
    });
    that.getlist();
  },

  
  bindTap2: function () {
    var that = this;
    this.setData({
      type: 1,
    });
    that.getlist();
  },
  bindTap3: function () {
    var that = this;
    this.setData({
      type: 2,
    });
    that.getlist();

  },

  getlist(){
    var url = app.globalData.SERVER+'repair/queryAllWithStateByCarer2';
    const shop = getObjectFromStorgeSync(KEYS.SHOP)
    var usetinfo = util.getData("userinfo");
    var usetinfo_id = usetinfo._id;

    var defaultCar = util.getData("defaultcar");
    const carcode = defaultCar.carcode
    var status= this.data.type;
    util.request(url, 'POST', {
      tel: shop.tel,
      state: status+'',
      page:'0',
      pagesize:'100',
      carerid: usetinfo_id,
      shopid: shop._id,
      carcode: carcode
    }).then(res => {
      util.showToast('成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1 ) {
        console.log(err.data.ret);
        var data = err.data.ret;
        for(var idx in data)
        {
          var data1= data[idx];
          var total = 0;
          var items = data1.items
          for (var index in items) {
            var item = items[index];
            var price = parseFloat(item.price);
            var num = parseInt(item.num);
            var workhourpay = item.workhourpay == '' ? 0 : parseFloat(item.workhourpay);

            total = total + (price + workhourpay) * num;
          }
          data[idx].total = total;
        }
        this.setData({
          infolist:data,
        });
        var  num = 1;
      } else {
        self.setData({
          siteInfo: err.data.site_info
        });
      }

     
    });
  }
})
